//
//  FZCircleProgressView.m
//  EnglishTalk
//
//  Created by CyonLeuMBP on 2017/1/20.
//  Copyright © 2017年 Feizhu Tech. All rights reserved.
//

#import "FZCircleProgressView.h"

@implementation FZCircleProgressView

#pragma mark - Lifecycle

- (id)init {
    return [self initWithFrame:CGRectMake(0.f, 0.f, 37.f, 37.f)];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
        _progress = 0.f;
        _annular = NO;
        _progressTintColor = [[UIColor alloc] initWithWhite:1.f alpha:1.f];
        _backgroundTintColor = [[UIColor alloc] initWithWhite:1.f alpha:.1f];
        _backgroundFillColor = [[UIColor alloc] initWithWhite:0.f alpha:0.5];
        
            //Set defaut sizes
        _progressRingWidth = 2.f;
        
            //Set up the progress layer
//        _progressLayer = [CAShapeLayer layer];
//        _progressLayer.strokeColor = [UIColor redColor].CGColor;
//        _progressLayer.fillColor = nil;//[UIColor clearColor].CGColor;
//        _progressLayer.lineCap = kCALineCapSquare;
//        _progressLayer.lineWidth = _progressRingWidth;
//        [self.layer addSublayer:_progressLayer];
        
        CALayer *bgLayer = [CALayer layer];
        self.bgFillLayer = bgLayer;
        self.bgFillLayer.backgroundColor = _backgroundFillColor.CGColor;
        self.bgFillLayer.masksToBounds = YES;
        
        [self.layer insertSublayer:self.bgFillLayer atIndex:0];
        
        [self registerForKVO];
    }
    return self;
}

- (void)setBackgroundFillColor:(UIColor *)backgroundFillColor {
    _backgroundFillColor = backgroundFillColor;
    if (self.bgFillLayer) {
        self.bgFillLayer.backgroundColor = _backgroundFillColor.CGColor;
    }
}

- (void)dealloc {
    [self unregisterFromKVO];
#if !__has_feature(objc_arc)
    [_progressTintColor release];
    [_backgroundTintColor release];
    [super dealloc];
#endif
}

#pragma mark - Drawing

//- (void)drawRect:(CGRect)rect {
//    
//    CGRect allRect = self.bounds;
//    CGFloat borderWidth = 2.0 / [UIScreen mainScreen].scale;
//    CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);
//    CGContextRef context = UIGraphicsGetCurrentContext();

//    if (_annular) {
//            // Draw background
//        BOOL isPreiOS7 = kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_7_0;
//        CGFloat lineWidth = isPreiOS7 ? 5.f : 2.f;
//        UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
//        processBackgroundPath.lineWidth = lineWidth;
//        processBackgroundPath.lineCapStyle = kCGLineCapButt;
//        CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
//        CGFloat radius = (self.bounds.size.width - lineWidth)/2;
//        CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
//        CGFloat endAngle = (2 * (float)M_PI) + startAngle;
//        [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
//        [_backgroundTintColor set];
//        [processBackgroundPath stroke];
//            // Draw progress
//        UIBezierPath *processPath = [UIBezierPath bezierPath];
//        processPath.lineCapStyle = isPreiOS7 ? kCGLineCapRound : kCGLineCapSquare;
//        processPath.lineWidth = lineWidth;
//        endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
//        [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
//        [_progressTintColor set];
//        [processPath stroke];
//    } else {
            // Draw background
//        [_progressTintColor setStroke];
//        [_backgroundTintColor setFill];
//        CGContextSetLineWidth(context, borderWidth);
////        CGContextFillEllipseInRect(context, circleRect);
//        CGContextStrokeEllipseInRect(context, circleRect);
////        CGContextDrawPath(context, kCGPathFillStroke);
//
//            // Draw progress
//        CGPoint startPoint = CGPointMake(allRect.size.width / 2, 0);
//        CGFloat radius = (allRect.size.width - 4) / 2;
//        CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
//        CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
////        [_progressTintColor setFill];
//        CGContextMoveToPoint(context, startPoint.x, startPoint.y);
////        CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
////        CGContextClosePath(context);
//        CGContextStrokePath(context);
////    }
//}

- (void)drawRect:(CGRect)rect
{
//    [super drawRect:rect];
    
        //Draw Progress
//    [self drawProgress];
    CGFloat borderWidth = _progressRingWidth;// / [UIScreen mainScreen].scale;

    CGFloat edgeSpace = (borderWidth);
    CGFloat fillWidth = CGRectGetWidth(self.bounds) -  edgeSpace* 2;
    self.bgFillLayer.frame = CGRectMake(edgeSpace, edgeSpace, fillWidth, fillWidth);
    self.bgFillLayer.cornerRadius = fillWidth / 2.f;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, borderWidth);
    [_backgroundTintColor setStroke];

        //绘制圆
//    CGRect frame = self.bounds;
//    CGContextFillRect(context, rect);
//    
//    CGContextAddEllipseInRect(context, frame);
//    [_backgroundFillColor set];
//    CGContextFillPath(context);
    
        //绘制默认背景
    float bgstartAngle = - M_PI_2;
    float bgendAngle = bgstartAngle + (2.0 * M_PI);
    CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.width / 2.0);
    CGFloat radius = (self.bounds.size.width - borderWidth) / 2.0;
    
    CGContextAddArc(context, center.x, center.y, radius, bgstartAngle, bgendAngle, 0);
        //绘制圆弧
    CGContextDrawPath(context, kCGPathStroke);
    
        //绘制进度
    float startAngle = - M_PI_2;
    float endAngle = startAngle + (2.0 * M_PI * self.progress);
//    CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.width / 2.0);
//    CGFloat radius = (self.bounds.size.width - borderWidth) / 2.0;
    [_progressTintColor setStroke];

    CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
        //绘制圆弧
    CGContextDrawPath(context, kCGPathStroke);
}

- (void)drawProgress
{
        //Create parameters to draw progress
    float startAngle = - M_PI_2;
    float endAngle = startAngle + (2.0 * M_PI * self.progress);
    CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.width / 2.0);
    CGFloat radius = (self.bounds.size.width - _progressRingWidth) / 2.0;
    
        //Draw path
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineCapStyle = kCGLineCapButt;
    path.lineWidth = _progressRingWidth;
    [path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    
        //Set the path
    [_progressLayer setPath:path.CGPath];
}

- (void)setProgress:(float)progress {
    if (_progress == progress) {
        return;
    }
    
    _progress = progress;
    [self setNeedsDisplay];
}


- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    
    self.bgFillLayer.frame = self.bounds;
    self.bgFillLayer.cornerRadius = CGRectGetWidth(self.bounds)/2.0;
}

#pragma mark - KVO

- (void)registerForKVO {
    for (NSString *keyPath in [self observableKeypaths]) {
        [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
    }
}

- (void)unregisterFromKVO {
    for (NSString *keyPath in [self observableKeypaths]) {
        [self removeObserver:self forKeyPath:keyPath];
    }
}

- (NSArray *)observableKeypaths {
    return [NSArray arrayWithObjects:@"progressTintColor", @"backgroundTintColor", @"progress", @"annular", nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    [self setNeedsDisplay];
}

@end
